/*
 *  Copyright 2009 Lucas Nazário dos Santos
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sourceforge.retriever.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import net.sourceforge.retriever.Retriever;
import net.sourceforge.retriever.analyzer.Analyzer;
import net.sourceforge.retriever.fetcher.Resource;
import net.sourceforge.retriever.filter.URLRegexFilter;

/*
 * This sample shows how to discover feeds inside Web sites.
 * 
 * It may take a while to find feeds because sites used in this sample
 * are quite large.
 */
public class CrawlingFeedsFromJournals {


	public static void main(final String[] args) throws MalformedURLException {

		final Retriever crawler = new Retriever();

		// Add seeds to online journals
		crawler.addSeed(new URL("http://www.washingtonpost.com/"));
		crawler.addSeed(new URL("http://www.nytimes.com/"));

		// Tells the crawler to stay within the predefined journals
		final URLRegexFilter filter = new URLRegexFilter();
		filter.addRegex(".+washingtonpost\\.com.+");
		filter.addRegex(".+nytimes\\.com.+");
		crawler.setFilter(filter);

		// Retains only feeds, printing their URLs
		crawler.setAnalyzer(new Analyzer() {
			public void analyze(final Resource resource, final Map<String, Object> additionalInfo) {
				try {
					final String content = this.getContent(resource);
					if (this.isRSSFeed(content)) {
						System.out.println(resource.getURL().toExternalForm());
					}
				} catch (final IOException e) {
					e.printStackTrace();
				}
			}

			private String getContent(final Resource resource) throws IOException {
				final StringBuilder result = new StringBuilder();
				BufferedReader reader = null;
				try {
					reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
					final char[] buffer = new char[1024];
					int length = 0;
					while ((length = reader.read(buffer, 0, buffer.length)) > 0) {
						result.append(buffer, 0, length);
					}
				} finally {
					if (reader != null) reader.close();
				}
				return result.toString();
			}

			private boolean isRSSFeed(final String content) {
				// This is a very weak way to discover if the content comes from a feed.
				// For production software, please refer to other techniques.
				return content.contains("<rss");
			}
		});

		crawler.start();
	}
}